package com.hbyj.db.factory;

import java.sql.Connection;
import com.hbyj.db.function.CConfig;
import com.hbyj.db.function.CDBInfo;


/**
 * 数据库连接类
 * 
 * @author Administrator
 * 
 */
public class CDBConnection {
	/**
	 * 获取数据库连接
	 * 
	 * @param DataBase
	 *            所用数据库，SQLServer
	 * @param DBService
	 *            数据库IP
	 * @param DatabaseName
	 *            数据库名
	 * @param UserName
	 *            用户名
	 * @param PassWord
	 *            密码
	 * @param DriverName 
	 *			  数据库JDBC驱动  		
	 * @return Connection
	 */
	/*
	 * public static String DBService = "192.168.83.240"; public static String
	 * DatabaseName = "GhGForecast"; public static String UserName = "sa";
	 * public static String PassWord = "guest";
	 */
	
	
	public static Connection con_sourse = null;
	public static Connection con_target = null;
	public static Connection getSQLConnection(CDBInfo dbInfo) {
		try {
			//CDBInfo dbInfo = CConfig.getSourceDB();//获取数据库配置信息

			// 装载驱动程序
			if (con_sourse != null || !con_sourse.isClosed()) {
				con_sourse.close();
				Class.forName(dbInfo.getDriverName())
						.newInstance();
				// 创建连接
				String url = dbInfo.getConnectionUrl().equals("")?
						"jdbc:jtds:sqlserver://" + dbInfo.getdBService()+":"+dbInfo.getdBPort()
						+ ";DatabaseName=" + dbInfo.getdBName():dbInfo.getConnectionUrl()+";useLOBs=false";
				con_sourse = java.sql.DriverManager.getConnection(url, dbInfo.getUserName(),
						dbInfo.getPassWord());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con_sourse;
	}
	
}
